package com.milling.common.utils.http;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.milling.common.utils.DateUtils;
import com.milling.common.utils.uuid.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class WmsApiUtils {

    private static final Logger logger = LoggerFactory.getLogger(WmsApiUtils.class);

    // 从参数中读取后库接口地址参数
    @Value("${wcs.tcp.host}")
    private String host;

    @Value("${wcs.tcp.macCode}")
    private String macCode2;

    @Value("${wcs.tcp.queryInventory}")
    private String queryInventory;

    @Value("${wcs.tcp.requestMaterial}")
    private String requestMaterial; // 从配置文件或命令行参数中读取

    @Value("${wcs.tcp.queryDrillinfoByBoxid}")
    private String queryDrillinfoByBoxid;

    private String macCode = "D001";

    private final String queryInventoryString = "http://192.168.237.200:5001/RestAPI/QueryInventory";
    private final String requestMaterialString = "http://192.168.237.200:5001/RestAPI/RequestMaterial";
    private final String queryDrillinfoByBoxidString = "http://192.168.237.200:5001/RestAPI/QueryDrillinfoByBoxid";

    private final String sortingInStockString = "http://192.168.237.200:5001/RestAPI/UploadDrill";

    public String getWmsApiUrl(String path) {
        return host + path;
    }

    public String queryInventory() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("MacCode", macCode);
        jsonObject.put("txn_id", UUID.randomUUID().toString());
        jsonObject.put("Timestamp", DateUtils.getTime());

        String jsonString = jsonObject.toJSONString();

        String result = HttpUtils.postData(queryInventoryString, jsonString);

        JSONObject jsonObject2 = JSONObject.parseObject(result);

        // 提取顶层字段
        String code = jsonObject2.getString("Code");
        String message = jsonObject2.getString("Message");

        if("0".equals(code)) {
            return result;
        } else {
            logger.info("查询失败，错误信息：" + message);
            return null;
        }
    }

    public Map<String, Integer> queryInventoryV1() {
        Map<String, Integer> stockMap = new HashMap<String, Integer>();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("MacCode", macCode);
        jsonObject.put("txn_id", UUID.randomUUID().toString());
        jsonObject.put("Timestamp", DateUtils.getTime());

        String jsonString = jsonObject.toJSONString();


//        String result = HttpUtils.postData(getWmsApiUrl(queryInventory), jsonString);

        String result = HttpUtils.postData(queryInventoryString, jsonString);
//        return result;

        JSONObject jsonObject2 = JSONObject.parseObject(result);
        // 解析JSON字符串

        // 提取顶层字段
        String code = jsonObject2.getString("Code");
        String message = jsonObject2.getString("Message");


        if("0".equals(code)) {
            // 提取DataList数组
            JSONArray dataList = jsonObject2.getJSONArray("DataList");
            if (dataList != null) {
                int total = 0;
                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject item = dataList.getJSONObject(i);
                    String drillInfo = item.getString("drill_info");
                    String drillCount = item.getString("drill_count");
                    String boxCount = item.getString("box_Count");

                    if (new Integer(drillCount).intValue() % 50 == 0) {
                        stockMap.put(drillInfo, new Integer(drillCount).intValue());
                    } else {

                    }
                }
            }
        } else {
            logger.info("查询失败，错误信息：" + message);
            return null;
        }
        logger.info(stockMap.toString());
        return stockMap;
    }

    public String queryDrillinfoByBoxid(String boxCode) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("box_id", boxCode);
        jsonObject.put("MacCode", "D001");
        jsonObject.put("txn_id", UUID.randomUUID().toString());
        jsonObject.put("Timestamp", DateUtils.getTime());

        String result = HttpUtils.postData(queryDrillinfoByBoxidString, jsonObject.toJSONString());
//        String result = HttpUtils.postData(getWmsApiUrl(queryDrillinfoByBoxid), jsonObject.toJSONString());
        return result;
    }

    public String requestMaterial(String reqBody) {

        String result = HttpUtils.postData(requestMaterialString, reqBody);
        return result;
    }

    public JSONObject genReqBody(String drillInfo, String deliver_port) {
        JSONObject jsonObject = new JSONObject();

//        jsonObject.put("MacCode", macCode);
        jsonObject.put("MacCode", "D001");
        jsonObject.put("txn_id", UUID.randomUUID().toString());
        jsonObject.put("Timestamp", DateUtils.getTime());


        jsonObject.put("m_Drill_Info", drillInfo);

        String drillSize = drillInfo.substring(0, 4);
        String drillType = "" + drillInfo.charAt(4);
        int drillCount = 50;
        int drillGrindCount = new Integer(drillInfo.charAt(9)).intValue();

        jsonObject.put("m_Drill_Count", drillCount);
        jsonObject.put("m_Drill_Box_Count", drillCount / 50);

        jsonObject.put("m_Drill_Type", drillType);
        jsonObject.put("m_Drill_Size", drillSize);
        jsonObject.put("m_Drill_Grind_Count", drillGrindCount);

        jsonObject.put("deliver_port", deliver_port);

        jsonObject.put("txn_id", java.util.UUID.randomUUID());

        jsonObject.put("m_Drill_Edge", "1");
        jsonObject.put("m_Drill_Factory", "1");
        jsonObject.put("m_Drill_Grind_Status", "1");
        jsonObject.put("m_Drill_Asc", "1");
        jsonObject.put("m_Drill_Box_Type", "01");
        return jsonObject;
    }

    public JSONObject genReqBodyDrillcount(String drillInfo, String deliver_port, Long drillCount) {
        JSONObject jsonObject = new JSONObject();

//        jsonObject.put("MacCode", macCode);
        jsonObject.put("MacCode", "D001");
        jsonObject.put("txn_id", UUID.randomUUID().toString());
        jsonObject.put("Timestamp", DateUtils.getTime());


        jsonObject.put("m_Drill_Info", drillInfo);

        String drillSize = drillInfo.substring(0, 4);
        String drillType = "" + drillInfo.charAt(4);
        int drillGrindCount = new Integer(drillInfo.charAt(9)).intValue();

        jsonObject.put("m_Drill_Count", drillCount * 50);
        jsonObject.put("m_Drill_Box_Count", drillCount);

        jsonObject.put("m_Drill_Type", drillType);
        jsonObject.put("m_Drill_Size", drillSize);
        jsonObject.put("m_Drill_Grind_Count", drillGrindCount);

        jsonObject.put("deliver_port", deliver_port);

        jsonObject.put("txn_id", java.util.UUID.randomUUID());

        jsonObject.put("m_Drill_Edge", "1");
        jsonObject.put("m_Drill_Factory", "1");
        jsonObject.put("m_Drill_Grind_Status", "1");
        jsonObject.put("m_Drill_Asc", "1");
        jsonObject.put("m_Drill_Box_Type", "01");
        return jsonObject;
    }

    public String sortingInStock(String jsonString) {
        String result = HttpUtils.postData(sortingInStockString, jsonString);

        return result;
        /*JSONObject jsonObject2 = JSONObject.parseObject(result);

        // 提取顶层字段
        String code = jsonObject2.getString("Code");
        String message = jsonObject2.getString("Message");

        if("0".equals(code)) {
            logger.info("通知WMS分拣入库钻针信息成功：" + message);
            return result;
        } else {
            logger.info("通知WMS分拣入库钻针信息失败，错误信息：" + message);
            return null;
        }*/
    }
}
